/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package anton.admin.reservation;

import anton.admin.reservation.DestroyReservation;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author Chris
 */
@ManagedBean(name = "reservationBean")
@SessionScoped
public class ReservationBean implements Serializable {

    /**
     * Creates a new instance of ReservationBean
     */

        @Resource(name = "jdbc/antonsDatasource") // data source to use for mysql queries

        private DataSource ds;

        ArrayList<Reservation> list;

        Reservation reservation;

    public List<Reservation> getReservations() throws SQLException {

        if (ds == null) {
            throw new SQLException("Can't get data source");// throws exception if datasource not available
        }
            try (Connection con = ds.getConnection()) {
                if (con == null) {
                    throw new SQLException("Can't get database connection");
                }
                PreparedStatement ps
                        = con.prepareStatement(
                                "Select reservation_ID, reservation_date, reservation_time, reservation_name, number_of_guests, reservation_phone, reservation_email  from Reservations"); // query select from stock specified attributes
                
                //get  item info from database
                ResultSet result = ps.executeQuery();
                
                list = new ArrayList<>();
                
                while (result.next()) {
                    
                    Reservation res = new Reservation();
                    
                    res.setReservation_ID(result.getInt("reservation_ID"));
                    res.setReservation_date(result.getDate("reservation_date"));
                    res.setReservation_time(result.getTime("reservation_time"));
                    res.setReservation_name(result.getString("reservation_name"));
                    res.setNumber_of_guests(result.getInt("number_of_guests"));
                    res.setReservation_phone(result.getString("reservation_phone"));
                    res.setReservation_email(result.getString("reservation_email"));
                    list.add(res);
                }   }
        return list;

    }

}
